<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>分校区网点</title>
    <link rel="stylesheet" href="${ctxPath}/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="${ctxPath}/assets/module/admin.css"/>
    <link rel="stylesheet" href="${ctxPath}/assets/libs/zTree/css/zTreeStyle/zTreeStyle.css"/>
</head>

<body>
<% include("../layout/loading.html"){} %>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
<!--                    <div class="layui-inline">-->
<!--                        <label class="layui-form-label w-auto">机构名称：</label>-->
<!--                        <div class="layui-input-inline mr0">-->
<!--                            <input id="edtSearch" class="layui-input" type="text" placeholder="机构名称"/>-->
<!--                        </div>-->
<!--                    </div>-->
                    <div class="layui-inline">
<!--                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>查询</button>-->
<!--                        <button id="btnReset" class="layui-btn icon-btn"><i class="layui-icon">&#xe669;</i>重置</button>-->
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>新增</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="schoolWebsiteTable" lay-filter="schoolWebsiteTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelSchoolWebsite">
    <form id="modelSchoolWebsiteForm" lay-filter="modelSchoolWebsiteForm" class="layui-form model-form" >
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>校区名称:</label>
            <div class="layui-input-block">
                <input name="schoolName" placeholder="请输入" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>

<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">所在地区:</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input name="schoolName" placeholder="请输入" type="text" class="layui-input" maxlength="20"-->
<!--                       lay-verType="tips" lay-verify="required" required/>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>所在地区:</label>
            <div class="layui-input-inline" style="width: 150px">
                <select name="provinceId" xm-select="provinceId" lay-verType="tips" lay-verify="required" lay-filter="schoolWebsiteType">
                    <option value="">请选择省</option>
                    <% for(province in provinceList) { %>
                    <option value="${province.code}">${province.name}</option>
                    <% } %>
                </select>
            </div>
            <div class="layui-input-inline" style="width: 150px">
                <select name="cityId" id="cityId" xm-select="cityId" lay-verType="tips" lay-verify="required" lay-filter="schoolWebsiteCityType">
                    <option value="">请选择市:</option>

                </select>
            </div>
            <div class="layui-input-inline" style="width: 200px">
                <select name="areaId" id="areaId" lay-verify="required">
                    <option value="">请选择县/区:</option>

                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>具体地址:</label>
            <div class="layui-input-block">
                <input name="address" placeholder="请输入" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>客服电话:</label>
            <div class="layui-input-block">
                <input name="telNo" placeholder="请输入" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required|Nnumber" required/>
            </div>
        </div>
        <div class="layui-upload">
            <label class="layui-form-label">图片上传:</label>
            <button type="button" class="layui-btn" id="manyImages" >上传</button>
            <div class="layui-upload-list" id="imageses"></div>
            <input id="schoolWebsiteImages" name="schoolWebsiteImages"  hidden/>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="schoolWebsiteSubmit" lay-submit>保存</button>
        </div>
    </form>
    <style>
        .js-img{width:100px;height:100px;position:relative;float:left;padding: 0 5px 5px 0}
        .js-img>img{width:100px;height:100px;}
        .js-img>span{cursor:pointer;font-weight:666;position:absolute;top:0;right:0;display:inline-block;text-align:center;width:24px;height:24px;line-height:24px;}
    </style>
</script>

<!-- js部分 -->
<script type="text/javascript" src="${ctxPath}/assets/libs/jquery/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/libs/zTree/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/js/common.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var upload = layui.upload;
        // 渲染表格
        var insTb = table.render({
            elem: '#schoolWebsiteTable',
            url: '/admin/schoolWebsite/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'schoolName', title: '校区名称'},
                {field: 'provinceNames', title: '所在地区'},
                {field: 'address', title: '具体地址'},
                {field: 'telNo', title: '客服电话'},
                // {field: 'provinceId', title: '省',hide: true,templet:function (data){
                //          console.log(data.provinceCode);
                //         return data.provinceCode;
                //     }},
                // {field: 'cityId', title: '市',hide: true,templet:function (data){
                //         return data.cityCode;
                //     }},
                // {field: 'areaId', title: '区',hide: true,templet:function (data){
                //         return data.areaCode;
                //     }},
                {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
            ]]
        });

        // 添加
        $('#btnAdd').click(function () {
            showEditModel();
        });

        // // 搜索
        // $('#btnSearch').click(function () {
        //     insTb.reload({where: {keyword: $('#edtSearch').val()}});
        // });

        // // 重置
        // $('#btnReset').click(function () {
        //     $('#edtSearch').val("");
        //     window.location.reload();
        // });

        // 工具条点击事件
        table.on('tool(schoolWebsiteTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(obj);
            }
        });

        // 删除
        function doDel(obj) {
            top.layer.confirm('确定要删除“' + obj.data.schoolName + '”分校区吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                top.layer.close(i);
                layer.load(2);
                $.post('/admin/schoolWebsite/delete', {
                    schoolWebsiteId: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }
        //联动,市
        form.on('select(schoolWebsiteType)', function(data){
            var val=data.value;
            var text = data.elem[data.elem.selectedIndex].text;
            // console.log(text);
            $.ajax({
                type: 'GET',
                url: '/admin/address/city/list',
                data: {code:val,type:1},
                dataType:  'json',
                success: function(data){
                    // console.log(data.data);
                    $("#cityId").empty();
                    $('#cityId').append(new Option("请选择", ""));// 下拉菜单里添加元素
                    $.each(data.data, function (index, item) {
                        $('#cityId').append(new Option(item.name, item.code));// 下拉菜单里添加元素
                    });
                    form.render("select");//重新渲染 固定写法
                }
            });
        });
        //联动,区
        form.on('select(schoolWebsiteCityType)', function(data){
            var val=data.value;
            var text = data.elem[data.elem.selectedIndex].text;
            // console.log(text);
            $.ajax({
                type: 'GET',
                url: '/admin/address/city/list',
                data: {code:val,type:2},
                dataType:  'json',
                success: function(data){
                    // console.log(data.data);
                    $("#areaId").empty();
                    $('#areaId').append(new Option("请选择", ""));// 下拉菜单里添加元素
                    $.each(data.data, function (index, item) {
                        $('#areaId').append(new Option(item.name, item.code));// 下拉菜单里添加元素
                    });
                    form.render("select");//重新渲染 固定写法
                }
            });
        });
        //自定义表单验证
        form.verify({
            Nnumber:[/(^$)|^\d+$/,'只能填写数字']
        });
        var previewImg = x => ["<div class='js-img'><img src='",x,"' class='layui-upload-img'/><span>X</span></div>"].join("");

            // 显示编辑弹窗
            function showEditModel(mRole) {
                console.log(mRole)
                admin.open({
                    type: 1,
                    title: (mRole ? '编辑' : '新增') + '分校区',
                    area: ['700px', '500px'],
                    content: $('#modelSchoolWebsite').html(),
                    success: function (layero, dIndex) {
                        var url = mRole ? '/admin/schoolWebsite/update' : '/admin/schoolWebsite/add';
                        if (mRole) {
                            $('#cityId').append(new Option("请选择", ""));// 下拉菜单里添加元素
                            $('#cityId').append(new Option(mRole.cityName, mRole.cityId));// 下拉菜单里添加元素
                            $('#areaId').append(new Option("请选择", ""));// 下拉菜单里添加元素
                            $('#areaId').append(new Option(mRole.areaName, mRole.areaId));// 下拉菜单里添加元素
                            form.render("select");//重新渲染 固定写法

                        }
                        form.val('modelSchoolWebsiteForm', mRole);  // 回显数据
                        // 表单提交事件
                        if(mRole&&mRole.schoolWebsiteImages)$('#imageses').html(mRole.schoolWebsiteImages.split(",").map(i=>previewImg(i)).join(""));

                        form.on('submit(schoolWebsiteSubmit)', function (data) {
                            layer.load(2);
                            var imgs = [];
                            $("#imageses img").each((k,v)=>imgs.push(v.src));
                            data.field.schoolWebsiteImages = imgs.join();

                            $.post(url, data.field, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    insTb.reload();
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'json');
                            return false;
                        });
                        $("body").on("click",".js-img>span",function(e){
                            $(this).closest(".js-img").remove();
                        });
                        var schoolWebsiteImages = "";
                        //多图片上传
                        upload.render({
                            elem: '#manyImages' //绑定点击按钮
                            , url: '/upload/uploadFile' //上传接口
                            , multiple: true  //确认上传多张图片
                            , number: 6 //最大上传图片数量
                            , before: function (obj) {
                                //预读本地文件示例，不支持ie8
                                // obj.preview(function (index, file, result) {
                                //     $('#imageses').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" height="100px" width="100px">')
                                // });
                            }
                            , done: function (res) {
                                console.log(1);
                                // if($('#imageses>*').length>5){
                                //     layer.msg("图片不允许超过6张！");
                                // }else{
                                    if(res.code==200&&res.msg)$('#imageses').append(previewImg(res.msg));
                                    //上传完毕
                                    if (schoolWebsiteImages == "") {
                                        schoolWebsiteImages = res.msg;
                                    } else {
                                        schoolWebsiteImages = schoolWebsiteImages + "," + res.msg;
                                    }
                                    ;
                                    // console.log(schoolWebsiteImages);
                                    $('#schoolWebsiteImages').val(schoolWebsiteImages);
                                // }
                            }
                        });
                    }
                });
            }
    });
</script>
</body>
</html>